#! /usr/bin/env python
#coding:utf-8
# 保证有对应的进程在跑
# feilong
# 说明 例子 hotfix manager


import os
import curses
import locale
import time
import sys
import socket
import StringIO
import threading
import subprocess


def checkPro(pro_type, pro_name):
    cmd = "ps -def | grep skynet | grep -v grep |grep "+pro_name+" | awk '{print $2}' "
    # print(cmd)
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    text = p.communicate()
    pid = None
    for id in text :
        if id == None :
            break
        # print(id.strip())
        pid = id.strip()
    if pid == "" :
        cmd = "sh ./run.sh "+pro_type+" "+pro_name +" &"
        print(cmd)
        subprocess.Popen(cmd, shell=True)



 
if __name__ == '__main__':
    try:
        while True :
            checkPro("ndlogs", "ndlogs")
            time.sleep(1)
            checkPro("ndgame", "ndmatch")
            checkPro("ndgame", "ndgame")
            time.sleep(1)
            checkPro("ndadmin", "ndadmin")
            checkPro("ndadmin", "ndbot")
            time.sleep(8)
    except:
        print("\nchecknode.py exit! \n")